package net.sxnic.ugr.group.action;

import net.sxnic.ugr.UgrActionSupport;
import net.sxnic.ugr.group.Group;
import net.sxnic.ugr.group.GroupManager;
import net.sxnic.ugr.role.Role;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;


@SuppressWarnings("serial")
public class Create extends UgrActionSupport {

    /**
     * commons logging
     */
    
	private static Logger logger = LoggerFactory.getLogger(Create.class);

    private Group group;

    private String parentId;

    @Autowired
    private GroupManager groupManager;
    
    /**
	 * 角色
	 */
	private String role;
	
	 
    /**
     * @see com.opensymphony.xwork2.ActionSupport#execute()
     */
    public String execute() throws Exception {
        checkRoles(Role.SYSTEM_ADMIN);

        try {
            groupManager.save(group);
            
            // 角色
     		if (StringUtils.isBlank(role)) {
     			group.getRoles().clear();
     			 
     		} else {
     			 
     			String[] roles = StringUtils.split(role, ",");
     			group.getRoles().clear();
     			Role r = null;
     			for (String rr : roles) {
     				r = authorizationManager.getRole(rr.trim());
     				logger.debug("role id :" + r.getId() + " , group id :"
     						+ group.getId());

     				group.getRoles().add(r);  			 
     			}			 
     		}      	
     	
        } catch (Exception eaee) {
//            addActionError(getText("group.error.groupAlreadyExists",
//                new String[] { group.getName() }));

            return INPUT;
        }

        // 保存成功信息并转向
//        addActionMessage(getText("group.saveSuccess", new String[] { group
//                .getName() }));

        return SUCCESS;
    }

    /**
     * @return the group
     */
    public Group getGroup() {
        return group;
    }

    /**
     * @param group the group to set
     */
    public void setGroup(Group group) {
        this.group = group;
    }

    /**
     * @return the parentId
     */
    public String getParentId() {
        return parentId;
    }

    /**
     * @param parentId the parentId to set
     */
    public void setParentId(String parentId) {
        this.parentId = parentId;
    }
    
    
    public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
}
